package com.qianfeng.zw.meilishuo.utils;

import android.support.v7.widget.RecyclerView;
import android.widget.AbsListView;
import android.widget.BaseAdapter;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jayqqaa12.abase.core.activity.AAdapter;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qianfeng.zw.meilishuo.bean.AppConn;
import com.qianfeng.zw.meilishuo.listener.OnCompleteLinstener;

import java.util.ArrayList;
import java.util.List;

/**
 * @Package com.qianfeng.zw.meilishuo.utils
 * @作 用:
 * @创 建 人: zhangwei
 * @日 期: 15/3/31 23:20
 * @修 改 人:
 * 期:
 */
public class HttpHelper {

    private List<HttpHandler> handlers;

    private HttpUtils httpUtils;

    private OnCompleteLinstener completeLinstener;

    public HttpHelper() {
        this(15000, (String) null);
    }

    public HttpHelper(int connTimeout) {
        this(connTimeout, (String) null);
    }

    public HttpHelper(String userAgent) {
        this(15000, (String) null);
    }

    public HttpHelper(int connTimeout, String userAgent) {
        handlers = new ArrayList<>();
        httpUtils = new HttpUtils(connTimeout, userAgent);
    }

    /**
     * @param url
     */
    public <T> void getDataFromServer(String url, final TypeReference<AppConn<T>> type) {
        HttpHandler<String> httpHandler = httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                AppConn<T> conn = JSONObject.parseObject(responseInfo.result, type);
                if (conn != null && completeLinstener != null) {
                    completeLinstener.onCompleteLinstener(conn.getData());
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
        handlers.add(httpHandler);
    }

    /**
     * @param method
     * @param list
     * @param adapter
     * @param type
     */
    public <T> void loadGoodsData(HttpRequest.HttpMethod method, String url, final List<T> list, final RecyclerView.Adapter adapter, final TypeReference<AppConn<T>> type) {
        HttpHandler<String> httpHandler = httpUtils.send(method, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                AppConn conn = JSONObject.parseObject(responseInfo.result, type);
                if (conn != null) {
                    list.addAll(conn.getData());
                    adapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
            }
        });
        handlers.add(httpHandler);
    }

    public <T> void loadData(HttpRequest.HttpMethod method, String url, final AAdapter<T> adapter, final TypeReference<AppConn<T>> type) {
        HttpHandler<String> httpHandler = httpUtils.send(method, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                AppConn conn = JSONObject.parseObject(responseInfo.result, type);
                if (conn != null) {
                    adapter.addData(conn.getData());
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
            }
        });
        handlers.add(httpHandler);
    }

    public  <T> void postData(String url, RequestParams params, final TypeReference<AppConn<T>> type) {
        httpUtils.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                AppConn<T> conn = JSONObject.parseObject(responseInfo.result, type);
                if (conn != null) {
                    if (completeLinstener != null && conn.getData() != null && !conn.getData().isEmpty()) {
                        completeLinstener.onCompleteLinstener(conn.getData());
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });


    }

    public <T> void loadRemmendData(HttpRequest.HttpMethod method, String url, final AbsListView absListView, final List<T> list, final BaseAdapter adapter, final TypeReference<AppConn<T>> type) {

        HttpHandler<String> httpHandler = httpUtils.send(method, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                AppConn conn = JSONObject.parseObject(responseInfo.result, type);
                if (conn != null) {
                    list.addAll(conn.getData());
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
            }
        });
        handlers.add(httpHandler);
    }

    public void cancel() {
        if (handlers != null && handlers.size() > 0) {
            for (HttpHandler httpHandler : handlers) {
                httpHandler.cancel();
            }
        }
    }

    public void setCompleteLinstener(OnCompleteLinstener completeLinstener) {
        this.completeLinstener = completeLinstener;
    }
}
